﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 获取文件修改时间
{
    public partial class Form1 : Form
    {
        public List<FileInfo> list = new List<FileInfo>();
        public Form1()
        {
            InitializeComponent();
            list = new List<FileInfo>();
            this.dateTimePicker1.Value = this.dateTimePicker2.Value = DateTime.Now;
            this.dataGridView1.AutoGenerateColumns = false;
          
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                GetFoldAll(this.textBox1.Text);
            }
            catch (Exception)
            {

                MessageBox.Show("查找不到该路径");
            }
            try
            {
                string[] noExtensions = this.textBox2.Text.Split(',');
                list = list.Where(p => !noExtensions.Contains(p.Extension)).OrderByDescending(p => p.LastWriteTime).ToList();
            }
            catch (Exception)
            {

                throw;
            }
            this.dataGridView1.DataSource = list;
            this.label5.Text = list.Count() + "条数据";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (list.Count > 0)
            {
                DateTime StartDate = this.dateTimePicker1.Value;
                DateTime EndDate = this.dateTimePicker2.Value;
                List<FileInfo> list2 = list.Where(p => p.LastWriteTime >= StartDate.Date && p.LastWriteTime.Date <= EndDate.Date).ToList();
                this.dataGridView1.DataSource = list2;
                this.label5.Text = list2.Count() + "条数据";
            }
            else
            {
                MessageBox.Show("请先获取数据");
            }
        }



        /// <summary> 
        /// 获取指定文件夹下所有子目录及文件 
        /// </summary> 
        /// <param name="Path">详细路径</param> 
        public void GetFoldAll(string Path)
        {
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            ListTreeShow(thisOne, 0);
        }
        /// <summary> 
        /// 获取指定文件夹下所有子目录及文件函数 
        /// </summary> 
        /// <param name="theDir">指定目录</param> 
        /// <param name="nLevel">默认起始值,调用时,一般为0</param> 
        /// <returns></returns> 
        public void ListTreeShow(DirectoryInfo theDir, int nLevel)//递归目录 文件 
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录 
            foreach (DirectoryInfo dirinfo in subDirectories)
            {

                FileInfo[] fileInfo = dirinfo.GetFiles(); //目录下的文件 
                foreach (FileInfo fInfo in fileInfo)
                {
                    list.Add(fInfo);
                }
                ListTreeShow(dirinfo, nLevel + 1);
            }
        }


    }
}
